package controllers;

import beans.*;
import models.*;
import play.mvc.Controller;
import play.mvc.With;
import utils.SessionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xieyicheng on 2015/2/4.
 */
@With(value = CheckLogin.class)
public class Concerns extends Controller {
    public static void myConcern(int page) {
        User users = SessionUtils.getLoginUser();
        long total = Concern.count("concernId = ?", users.id);
        page = page <= 0 ? 1 : page;
        PageBean pageBean = PageBean.getInstance(page, total, 7);
        List<Concern> concern = Concern.find("concernId = ? order by createTime desc",users.id).
                fetch(pageBean.getCurPage(),pageBean.getPerPage());
        List<User> user = new ArrayList<User>();
        for(int i = 0;i < concern.size();i++)
        {
            User u = User.findById(concern.get(i).beConcernId);
            if(u == null)
                continue;
            user.add(u);
        }
        WebServiceBean webServiceBean = WebServiceBean.getSuccessInstance();
        webServiceBean.data = UserBean.bulidList(user);

//        renderJSON(webServiceBean);
        render(webServiceBean,pageBean);
    }
    public static void concernMe(int page) {
        String userId = SessionUtils.getUserId();
        long total = Concern.count("beConcernId = ?",userId);
        page = page <= 0 ? 1 : page;
        PageBean pageBean = PageBean.getInstance(page, total, 8);
        List<Concern> concernList = Concern.find("beConcernId = ? order by create_time desc", userId).
                fetch(pageBean.getCurPage(), pageBean.getPerPage());
        List<User> user = new ArrayList<User>();
        for(int i = 0;i < concernList.size();i++)
        {
            User u = User.findById(concernList.get(i).concernId);
            if(u == null)
                continue;
            user.add(u);
        }
        WebServiceBean webServiceBean = WebServiceBean.getSuccessInstance();
        webServiceBean.data = UserBean.bulidList(user);
        render(webServiceBean, pageBean);
        //render();
    }
    public static void concernMan(String userId,int page) {
        User user = User.findById(userId);
        long total = Trend.count("owner_id = ?", userId);
        page = page <= 0 ? 1 : page;
        PageBean pageBean = PageBean.getInstance(page, total, 3);
        List<Trend> trendList = Trend.find("owner_id = ? order by createTime desc", userId).
                fetch(pageBean.getCurPage(), pageBean.getPerPage());
        WebServiceBean webServiceBean = WebServiceBean.getSuccessInstance();
        webServiceBean.data = TrendBean.bulidList(trendList);
        render(user,webServiceBean);
    }
    public static void concernWork(String userId,int page) {
        User user = User.findById(userId);

        long total = Article.count("authorId = ? and isDelete != 1", userId);
        page = page <= 0 ? 1 : page;
        PageBean pageBean = PageBean.getInstance(page, total, 6);
        List<Article> articlelList = Article.find(
                "authorId = ? and isDelete != 1 order by weight , createTime desc",userId).fetch(
                pageBean.getCurPage(), pageBean.getPerPage());
        WebServiceBean webServiceBean = WebServiceBean.getInstance(1);
        webServiceBean.data = ArticleBean.buildList(articlelList);

        long T_total = Trend.count("owner_id = ?", userId);
        PageBean T_pageBean = PageBean.getInstance(1, T_total, 3);
        List<Trend> trendList = Trend.find("owner_id = ? order by createTime desc", userId).
                fetch(T_pageBean.getCurPage(), T_pageBean.getPerPage());
        WebServiceBean T_webServiceBean = WebServiceBean.getSuccessInstance();
        T_webServiceBean.data = TrendBean.bulidList(trendList);
        render(user,webServiceBean,pageBean,T_webServiceBean);
    }
    public static void concernAlbum(String userId,int page) {
        User user = User.findById(userId);
        long total = Album.count("authorId = ? and isDelete != 1", userId);
        page = page <= 0 ? 1 : page;
        PageBean pageBean = PageBean.getInstance(page, total, 6);
        List<Album> albumList = Album.find(
                "authorId = ? and isDelete != 1 order by createTime desc",userId).fetch(
                pageBean.getCurPage(), pageBean.getPerPage());
        WebServiceBean webServiceBean = WebServiceBean.getSuccessInstance();
        webServiceBean.data = AlbumBean.bulidList(albumList);
        render(user,webServiceBean,pageBean);
    }
    
    public static void myTrend(int page) {
        String userId = SessionUtils.getUserId();
        long total = Trend.count("owner_id = ?",userId);
        page = page <= 0 ? 1 : page;
        PageBean pageBean = PageBean.getInstance(page, total, 8);
        List<Trend> trendList = Trend.find("owner_id = ? order by createTime desc", userId).
                fetch(pageBean.getCurPage(), pageBean.getPerPage());
        WebServiceBean webServiceBean = WebServiceBean.getSuccessInstance();
        webServiceBean.data = TrendBean.bulidList(trendList);
        render(webServiceBean,pageBean);
    }
    public static void concernTrend(int page) {
        String userId = SessionUtils.getUserId();
        long total = Trend.count("select count(*) from Trend trend where trend.owner_id in (select beConcernId from Concern concern where concernId = ?)",userId);
        page = page <= 0 ? 1 : page;
        PageBean pageBean = PageBean.getInstance(page, total, 8);
        List<Trend> trendList = Trend.find("select trend from Trend trend where trend.owner_id in (select beConcernId from Concern concern where concernId = ?)  order by Trend.createTime desc", userId).
                fetch(pageBean.getCurPage(), pageBean.getPerPage());
        WebServiceBean webServiceBean = WebServiceBean.getSuccessInstance();
        webServiceBean.data = TrendBean.bulidList(trendList);
        render(webServiceBean, pageBean);
    }

}
